package cn.tedu.system.review.service.impl;


import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.order.mapper.OrderMapper;
import cn.tedu.system.review.mapper.ReviewMapper;
import cn.tedu.system.review.pojo.dto.*;
import cn.tedu.system.review.pojo.entity.Review;
import cn.tedu.system.review.pojo.vo.ReviewSelectVO;
import cn.tedu.system.review.service.ReviewService;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private OrderMapper orderMapper;

    //用户评论功能
    @Override
    public void addReview(ReviewAddDto reviewAddDto, HttpSession session) {
        log.debug("用户评价业务: {}", reviewAddDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Review review = new Review();
        BeanUtils.copyProperties(reviewAddDto, review);
        review.setUserId(userVO.getId());
        review.setCreateTime(new Date());
        review.setUpdateTime(new Date());
        //获取商家id
        Long productId = reviewAddDto.getProductId();
        Long merchantId = orderMapper.getMerchantId(productId);
        review.setMerchantId(merchantId);

        if (reviewMapper.addReview(review)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //用户删除评价功能
    @Override
    public void deleteReview(Long productId, HttpSession session) {
        log.debug("用户删除业务: {}", productId);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        ReviewDeleteDto reviewDeleteDto = new ReviewDeleteDto();
        reviewDeleteDto.setProductId(productId);
        reviewDeleteDto.setUserId(userVO.getId());

        if (reviewMapper.deleteReview(reviewDeleteDto)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //商家回复功能
    @Override
    public void replyReview(ReviewReplayDto reviewReplayDto, HttpSession session) {
        log.debug("商家回复业务: {}", reviewReplayDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        Review review = new Review();
        BeanUtils.copyProperties(reviewReplayDto, review);
        review.setReplyTime(new Date());

        if (reviewMapper.replyReview(review)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }

    }

    //商家删除回复功能
    @Override
    public void delMerReview(ReviewMerDelDto reviewMerDelDto, HttpSession session) {
        log.debug("商家删除回复业务: {}", reviewMerDelDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        if (reviewMapper.delMerReview(reviewMerDelDto)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //用户修改评价功能
    @Override
    public void updateReview(ReviewUpdateDto reviewUpdateDto, HttpSession session) {
        log.debug("用户修改评价业务: {}", reviewUpdateDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Review review = new Review();
        BeanUtils.copyProperties(reviewUpdateDto, review);
        review.setUserId(userVO.getId());
        review.setUpdateTime(new Date());

        if (reviewMapper.updateReview(review)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //商家修改回复
    @Override
    public void merUpdateReview(ReviewMerUpdateDto reviewMerUpdateDto, HttpSession session) {
        log.debug("商家修改回复业务: {}", reviewMerUpdateDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Review review = new Review();
        BeanUtils.copyProperties(reviewMerUpdateDto, review);
        review.setReplyTime(new Date());

        if (reviewMapper.merUpdateReview(review)<=0) {
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //显示评价功能
    @Override
    public List<ReviewSelectVO> selectReview(Long productId,HttpSession session) {
        List<ReviewSelectVO> list = reviewMapper.selectReview(productId);
        if (list == null && list.size() == 0) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }
}


